/*
 * SubSonic - http://subsonicproject.com
 * 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an 
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
*/

using System;
using System.Data;

namespace SubSonic.QueryEngine
{
    public enum ConstraintType
    {
        Where,
        And,
        Or
    }

    /// <summary>
    /// Summary for the Constraint class
    /// </summary>
    public class Constraint
    {
        /// <summary>
        /// 
        /// </summary>
        public SqlQuery query;


        #region props

        private string columnName;
        private Comparison comp;
        private ConstraintType condition = ConstraintType.Where;
        private DbType dbType;
        private string parameterName;
        private object paramValue;

        private string tableName;

        /// <summary>
        /// Gets or sets the condition.
        /// </summary>
        /// <value>The condition.</value>
        public ConstraintType Condition
        {
            get { return condition; }
            set { condition = value; }
        }

        /// <summary>
        /// Gets or sets the name of the table.
        /// </summary>
        /// <value>The name of the table.</value>
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        /// <summary>
        /// Gets or sets the name of the column.
        /// </summary>
        /// <value>The name of the column.</value>
        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }

        /// <summary>
        /// Gets or sets the comparison.
        /// </summary>
        /// <value>The comparison.</value>
        public Comparison Comparison
        {
            get { return comp; }
            set { comp = value; }
        }

        /// <summary>
        /// Gets or sets the parameter value.
        /// </summary>
        /// <value>The parameter value.</value>
        public object ParameterValue
        {
            get { return paramValue; }
            set { paramValue = value; }
        }

        /// <summary>
        /// Gets or sets the name of the parameter.
        /// </summary>
        /// <value>The name of the parameter.</value>
        public string ParameterName
        {
            get { return parameterName ?? ColumnName; }
            set { parameterName = value; }
        }

        /// <summary>
        /// Gets or sets the type of the db.
        /// </summary>
        /// <value>The type of the db.</value>
        public DbType DbType
        {
            get { return dbType; }
            set { dbType = value; }
        }

        /// <summary>
        /// Gets the comparison operator.
        /// </summary>
        /// <param name="comp">The comp.</param>
        /// <returns></returns>
        public static string GetComparisonOperator(Comparison comp)
        {
            string sOut;
            switch(comp)
            {
                case Comparison.Blank:
                    sOut = SqlComparison.BLANK;
                    break;
                case Comparison.GreaterThan:
                    sOut = SqlComparison.GREATER;
                    break;
                case Comparison.GreaterOrEquals:
                    sOut = SqlComparison.GREATER_OR_EQUAL;
                    break;
                case Comparison.LessThan:
                    sOut = SqlComparison.LESS;
                    break;
                case Comparison.LessOrEquals:
                    sOut = SqlComparison.LESS_OR_EQUAL;
                    break;
                case Comparison.Like:
                    sOut = SqlComparison.LIKE;
                    break;
                case Comparison.NotEquals:
                    sOut = SqlComparison.NOT_EQUAL;
                    break;
                case Comparison.NotLike:
                    sOut = SqlComparison.NOT_LIKE;
                    break;
                case Comparison.Is:
                    sOut = SqlComparison.IS;
                    break;
                case Comparison.IsNot:
                    sOut = SqlComparison.IS_NOT;
                    break;
                default:
                    sOut = SqlComparison.EQUAL;
                    break;
            }
            return sOut;
        }

        #endregion


        /// <summary>
        /// Determines whether [is greater than] [the specified val].
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsGreaterThan(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is less than] [the specified val].
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsLessThan(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is greater than or equal to] [the specified val].
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsGreaterThanOrEqualTo(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is less than or equal to] [the specified val].
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsLessThanOrEqualTo(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is not null] [the specified val].
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsNotNull(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether the specified val is null.
        /// </summary>
        /// <param name="val">The val.</param>
        /// <returns></returns>
        public SqlQuery IsNull(object val)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is between and] [the specified val1].
        /// </summary>
        /// <param name="val1">The val1.</param>
        /// <param name="val2">The val2.</param>
        /// <returns></returns>
        public SqlQuery IsBetweenAnd(object val1, object val2)
        {
            return query;
        }

        /// <summary>
        /// Determines whether [is between and] [the specified date1].
        /// </summary>
        /// <param name="date1">The date1.</param>
        /// <param name="date2">The date2.</param>
        /// <returns></returns>
        public SqlQuery IsBetweenAnd(DateTime date1, DateTime date2)
        {
            return query;
        }
    }
}